﻿using ConsoleTables;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Utils;

namespace Service.Impl
{
    public class DisplayServiceImpl: BaseServiceImpl<string, object>, DisplayService
    {
        public string Version()
        {
            Assembly asm = Assembly.Load("NetManagerSystem");
            Stream s = asm.GetManifestResourceStream("NetManagerSystem.version.json");
            using (StreamReader sr = new StreamReader(s))
            {
                string json = sr.ReadToEnd();
                var v = JsonConvert.DeserializeObject<Model.Version.Version>(json);
                var ver = v.HistoryVersions.Where(ve => ve.Version == v.CurrentVersion).First();
                var desc = string.Join("\r\n", ver.Description.ToArray());
                return $"当前版本：{ver.Version} 创建日期: {ver.Date}\r\n{desc}";
            }
        }
        public string Display()
        {
            // 全局视图，显示所有接口信息
            if (this.CommandContext.IsGlobalView)
            {
                var interfaces = NetworkUtil.ListNetworkInterface();
                return DisplayUtil.dispalyInterfaces(interfaces);
            }
            // 接口视图，显示接口配置信息
            if (this.CommandContext.IsIfView)
            {
                var inter = NetworkUtil.GetWorkInterface(this.CommandContext.Interface);
                return DisplayUtil.displayInterface(inter);
            }
            return "";
        }
    }
}
